import 'package:acgn_pioneer/components/circle_tag.dart';
import 'package:acgn_pioneer/plugin/dialog/src/constants.dart';
import 'package:acgn_pioneer/utils/pop_util.dart';
import 'package:acgn_pioneer/widgets/reader/author.dart';
import 'package:expandable_text/expandable_text.dart';
import 'package:flutter/widgets.dart';

import '../../public.dart';
import 'catalog.dart';
import 'events/_index.dart';
import 'mixins/widget_base.dart';

/// 详情
class ReaderDetail extends StatefulWidget {
  /// 插槽自有数据
  final dynamic data;

  /// 简单模式，没有目录和一些边框
  final bool simple;

  ReaderDetail({Key key, this.data, this.simple = false}) : super(key: key);

  @override
  ReaderDetailState createState() => new ReaderDetailState();
}

class ReaderDetailState extends State<ReaderDetail> with ReaderWidgetBase {
  @override
  void initState() {
    super.initState();
    if (!widget.simple) {
      // 简单模式下，不进行窗体size回调
      WidgetsBinding.instance.addPostFrameCallback((callback) {
        readerPage.bus.fire(ReaderChapterDetailSlotEvent(context.size));
      });
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    ChapterModel chapter = widget.data;
    ComicModel comicInfo = chapter.comicModel;
    var simple = widget.simple;
    return Container(
      width: double.infinity,
      padding: EdgeInsets.all(simple ? 0 : 16),
      decoration: BoxDecoration(color: Color(0xff1B1B26)),
      child: Column(children: [
        ClipRRect(
            borderRadius: BorderRadius.circular(8),
            child: Stack(
              children: [
                AspectRatio(
                  aspectRatio: 1.8,
                  child: Image.network(
                    comicInfo.coverImg,
                    fit: BoxFit.cover,
                  ),
                ),
                Positioned(
                  left: 0,
                  right: 0,
                  bottom: 0,
                  child: Container(
                      height: 80,
                      padding: EdgeInsets.only(left: 12, bottom: 10, right: 12),
                      decoration: BoxDecoration(
                        gradient: LinearGradient(
                            begin: Alignment.bottomCenter,
                            end: Alignment.topCenter,
                            colors: [
                              Color.fromARGB(200, 0, 0, 0),
                              Color.fromARGB(0, 0, 0, 0)
                            ]),
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          Row(children: [
                            Container(
                              margin: EdgeInsets.only(right: 10),
                              constraints: BoxConstraints(maxWidth: Rem(140)),
                              child: Text(
                                comicInfo.name,
                                overflow: TextOverflow.ellipsis,
                                style: TextStyle(
                                    fontSize: 18,
                                    color: ColorUtil.rgba(255, 255, 255)),
                              ),
                            ),
                            CircleTag(
                                text: comicInfo.copyrightType,
                                textColor: Colors.white,
                                bgColor: ColorUtil.rgba(254, 45, 102, 1)),
                            Expanded(
                              child: InkWell(
                                  onTap: () {
                                    PopUtil.show(context,
                                        ReaderAuthorDetail(comicInfo: comicInfo),
                                        alignment: Alignment.bottomCenter,
                                        transition: DialogTransition(
                                            duration:
                                                Duration(milliseconds: 400),
                                            type: AnimationType.fromBottom));
                                  },
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.end,
                                    children: [
                                      Container(
                                          child: Text(
                                        comicInfo.authorName,
                                        overflow: TextOverflow.ellipsis,
                                        style: TextStyle(
                                            color: Colors.white, fontSize: 14),
                                      )),
                                      SVG.icon('arrow_right',
                                          size: 14, color: Colors.white),
                                    ],
                                  )),
                            ),
                          ]),
                          Container(
                              margin: EdgeInsets.only(top: 10),
                              child: Row(children: [
                                comicInfo.tagStr != null
                                    ? CircleTag(
                                        text: comicInfo.tagStr,
                                        textColor:
                                            ColorUtil.rgba(51, 51, 51, 1),
                                        bgColor:
                                            ColorUtil.rgba(255, 215, 59, 1))
                                    : Container(),
                                CircleTag(
                                    text: comicInfo.sortTypeStr,
                                    textColor: ColorUtil.rgba(51, 51, 51, 1),
                                    bgColor: ColorUtil.rgba(255, 215, 59, 1)),
                                CircleTag(
                                    text:
                                        '${StringUtil.numberToUnitStr(comicInfo.totalCollect)}人追更',
                                    textColor: ColorUtil.rgba(51, 51, 51, 1),
                                    bgColor: ColorUtil.rgba(255, 215, 59, 1)),
                              ]))
                        ],
                      )),
                )
              ],
            )),
        Container(
          alignment: Alignment.topLeft,
          margin: EdgeInsets.only(top: 16, bottom: 16),
          child: ExpandableText(comicInfo.desc,
              expandText: '展开',
              collapseText: '',
              maxLines: 4,
              style: TextStyle(
                  color: ColorUtil.rgba(255, 255, 255, 0.6), fontSize: 14),
              linkColor: ColorUtil.rgba(100, 145, 217, 1)),
        ),
        !simple
            ? Container(
                alignment: Alignment.topLeft,
                margin: EdgeInsets.only(top: 8),
                child: Row(
                  children: [
                    Text(chapter.name,
                        style: TextStyle(color: Colors.white, fontSize: 16)),
                    SizedBox(width: Rem(8)),
                    ReaderCatalog(
                      bus: readerPage.bus,
                      comicIds: readerMainPage.comicIds,
                      comicInfo: comicInfo,
                      currentChpaterId: chapter.id,
                      textStyle: TextStyle(
                          color: ColorUtil.rgba(255, 255, 255, 0.6),
                          fontSize: 14),
                    )
                  ],
                ))
            : Container()
      ]),
    );
  }
}
